<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索框部分 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格部分 -->
      <el-table :data="orderData" style="width: 100%" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="120px"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="120px">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status == 0">未发货</el-tag>
            <el-tag type="success" v-else>已发货</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="120px"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">{{scope.row.create_time | formDate}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="editOrderdialog = true"></el-button>
          <el-button type="success" icon="el-icon-location-outline" size="mini" @click="orderMsg"></el-button>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryOrder.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryOrder.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 点击编辑的弹出框 -->
    <el-dialog title="修改地址" :visible.sync="editOrderdialog" width="50%">
      <!-- 表单部分 -->
      <el-form
        :model="editOrderForm"
        :rules="editOrderRules"
        ref="editOrderForm"
        label-width="100px"
        @close="editFormClose"
      >
        <el-form-item label="省市区/县" prop="name">
          <!-- 省市区级联选择器 -->
          <el-cascader v-model="editOrderForm.name" :options="cityData"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="name">
          <el-input v-model="editOrderForm.addr"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editOrderdialog = false">取 消</el-button>
        <el-button type="primary" @click="editOrderdialog = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 点击查看物流信息的弹出框 -->
    <el-dialog title="物流进度" :visible.sync="orderMsgDialog" width="50%">
      <!-- 时间线 -->
      <el-timeline :reverse="reverse">
        <el-timeline-item
          v-for="(activity, index) in orderMessage"
          :key="index"
          :timestamp="activity.ftime"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="orderMsgDialog = false">取 消</el-button>
        <el-button type="primary" @click="orderMsgDialog = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getOrderListApi, orderMsgApi } from "../api/index.js";
import cityData from "./citydata.js";
export default {
  data: function() {
    return {
      // 获取订单列表需要的参数
      queryOrder: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      // 订单列表数据
      orderData: [],
      // 分页总页数
      total: 0,
      // 控制编辑弹出框的显示与隐藏
      editOrderdialog: false,
      // 编辑部分的表单数据
      editOrderForm: {
        name: [],
        addr: ""
      },
      // 表单数据的验证规则
      editOrderRules: {
        name: [{ required: true, message: "请输入省市区", trigger: "blur" }],
        addr: [{ required: true, message: "请输入详细地址", trigger: "blur" }]
      },
      // 省市区联动数据
      cityData,
      // 物流信息的弹出框的显示与隐藏
      orderMsgDialog: false,
      // 物流订单信息数据
      orderMessage: [],
      reverse: false
    };
  },
  created() {
    this.getAllOrderList();
  },
  methods: {
    async getAllOrderList() {
      const { data: res } = await getOrderListApi(this.queryOrder);
      if (res.meta.status !== 200)
        return this.$message.error("订单列表获取失败");
      this.orderData = res.data.goods;
      this.total = res.data.total;
      this.$message.success("订单列表获取成功");
      console.log(res);
    },
    handleSizeChange(val) {
      this.queryOrder.pagesize = val;
      this.getAllOrderList();
    },
    handleCurrentChange(val) {
      this.queryOrder.pagenum = val;
      this.getAllOrderList();
    },
    // 关闭弹出框重置表单
    editFormClose() {
      this.$refs.editOrderForm.resetFields();
    },
    orderMsg() {
      this.getOrderMsg();
      this.orderMsgDialog = true;
    },
    // 查看物流信息
    async getOrderMsg() {
      const { data: res } = await orderMsgApi();
      if (res.meta.status !== 200)
        return this.$message.error("物流信息获取失败");
      this.orderMessage = res.data;
    }
  }
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
